package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionToRepair;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 陈炜
 * @Date: 2019/1/24 16:19
 * @Description:
 */
@Repository("inspectionToRepairDao")
public class InspectionToRepairDao implements CrudDao<InspectionToRepair> {
    private final static Logger logger = LoggerFactory.getLogger(InspectionToRepairDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String repairCode = queryHash.get("repairCode");
        if (!StringUtils.isEmpty(repairCode)) {
            conditionHash.put("(s.repairCode = ?{paramIndex}) ", repairCode);
        }
        String inspectionCode = queryHash.get("inspectionCode");
        if(!StringUtils.isEmpty(inspectionCode)){
            conditionHash.put("(s.inspectionCode = ?{paramIndex})",inspectionCode);
        }
        String contentId = queryHash.get("contentId");
        if(!StringUtils.isEmpty(contentId)){
            conditionHash.put("(s.contentId = ?{paramIndex})", StringConverters.null2Long(contentId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from InspectionToRepair s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionToRepair> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from InspectionToRepair s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionToRepair> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionToRepair.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionToRepair findModel(Long id) {
        return entityManager.find(InspectionToRepair.class, id);
    }

    @Override
    public Integer add(InspectionToRepair model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InspectionToRepairDaoImpl添加inspectionToRepair成功！");
        return 1;
    }

    @Override
    public Integer update(InspectionToRepair model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionToRepair inspectionToRepair = entityManager.find(InspectionToRepair.class, id);
        entityManager.remove(inspectionToRepair);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionToRepair model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
